<% 
  pg_config = @pg.user_config || {}
  pgbouncer_config = @pg.pgbouncer_user_config || {}

  errors = flash.now["errors"] || {}
  old = request.POST || {}

  unless old.empty?
    pg_keys = old["pg_config_keys"] || []
    pg_values = old["pg_config_values"] || []
    pgbouncer_keys = old["pgbouncer_config_keys"] || []
    pgbouncer_values = old["pgbouncer_config_values"] || []

    pg_config = pg_keys.zip(pg_values).to_h
    pgbouncer_config = pgbouncer_keys.zip(pgbouncer_values).to_h
  end
%>

<form action="<%= "#{@project_data[:path]}#{@pg.path}/config" %>" role="form" method="POST" class="min-w-2/3 pg-config">
  <%== csrf_tag("#{@project_data[:path]}#{@pg.path}/config") %>

  <%== part("components/config_card", title: "PostgreSQL Configuration", config: pg_config, extra_class: "pg-config-card", name: "pg_config", errors: errors) %>
  <%== part("components/config_card", title: "PgBouncer Configuration", config: pgbouncer_config, extra_class: "pgbouncer-config-card", name: "pgbouncer_config", errors: errors) %>
  <div class="flex justify-end">
    <%== part("components/form/submit_button", text: "Save", extra_class: "save-config-btn") if @edit_perm %>
  </div>
</form>
